package com.icode.web.filter;

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * User: alexz
 * Date: 14-11-11
 * Time: 上午10:40
 */
public class RewriteHtmlPageStrategy implements RewriteStrategy {

    @Override
    public boolean support(ServletResponse response, String content) {
        String contentType = response.getContentType();
        return contentType != null && contentType.contains("text/html") && content.contains("</body>");
    }

    @Override
    public String rewrite(String content) throws IOException {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("common-banner.html");
        String template = IOUtils.toString(inputStream);
        return content.replaceAll("</body>", template + "</body>");
    }
}
